const path = require('path')
const fse = require('fs-extra')

const { SupportedStyleSheets } = require('./consts')

// 向package.json添加自定义字段
module.exports = {
	apply: ({ styleSheets }) => {
		const pkgFile = path.resolve(process.cwd(), './package.json')
		const pkg = fse.readJsonSync(pkgFile)

		pkg.scripts = Object.assign({}, pkg.scripts, {
			c: 'cz'
		})

		pkg.husky = {
			hooks: {
				'pre-commit': 'lint-staged',
				'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS'
			}
		}
		pkg.config = Object.assign({}, pkg.config, {
			commitizen: {
				path: 'cz-conventional-changelog'
			}
		})

		pkg['lint-staged'] = {
			'*.{js,ts,tsx,jsx}': 'eslint'
		}
		if (styleSheets !== SupportedStyleSheets.others) {
			pkg['lint-staged'][`*.{css,less,scss}`] = 'stylelint'
		}

		fse.writeJsonSync(pkgFile, pkg, { spaces: '\t' })
	}
}
